// miniprogram/pages/tea-information/tea-information.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tea_name:null,
        tea_id:null,
        tea_xy:null,
        tea_level:null,
        input_boolean:true,
        bindtap_name:"tea",
        tea_modify:"修改个人信息",
        right_color:"rgb(148, 148, 148)",
        avatarUrl : '',
        teacher_id : ''
    },
    input_name: function(l) {
      this.setData({
        new_name:l.detail.value,
      })},
      input_id: function(l) {
        this.setData({
          new_id:l.detail.value,
        })},
        input_xy: function(l) {
          this.setData({
            new_xy:l.detail.value,
          })},
          input_level: function(l) {
            this.setData({
              new_level:l.detail.value,
            })},

    tea(){
        this.setData({
          input_boolean:false,
          right_color: "black",
          tea_modify:"修改完成",
          bindtap_name:"tea_yes"
        })
      },

    tea_yes(){
      wx.cloud.callFunction({
        name:"database_connect",
        data:{
          function_name:"update",
          name_collection:"teacher",
          condition:{teacher_id:that.data.teacher_id},
          data:{
            user_name:this.data.new_name,
            teacher_id:this.data.new_id,
            xy:this.data.new_xy,
            level:this.data.new_level
        }
        },
        success(res){
          console.log("success",res)
          wx.showToast({
            title: '修改成功！',
            icon: 'success',
            duration: 2000//持续的时间
          })
        },//成功的回调
        fail(res){
          console.log("fail",res)
          wx.showToast({
            title: '修改失败！',
            icon: 'fail',
            duration: 2000//持续的时间
          })
        }//失败的回调
      })
      this.setData({
        input_boolean:true,
        right_color: "rgb(148, 148, 148)",
        tea_modify:"修改个人信息",
        bindtap_name:"tea"
      })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that=this

        /*wx.cloud.callFunction({
          name:"database_connect",
          data:{
            function_name:"add",
            name_collection:"teacher",
            data:{
                _id:"o0mCy4vasdasd7B37dn",
                user_name:"林霆乐",
                teacher_id:"20181009",
                xy:"计算机工程学院",
                level:"2"
            }
          },
          success(res){
            console.log("success",res)
          },//成功的回调
          fail(res){
            console.log("fail",res)
          }//失败的回调
        })*/
        that.setData({   //获取上个页面传过来的userInfo用户信息
          avatarUrl: options.avatarUrl,
          teacher_id: options.teacher_id
        });
        wx.cloud.callFunction({
            name:"database_connect",
            data:{
              function_name:"findone",
              name_collection:"teacher",
              condition:{teacher_id:that.data.teacher_id}
            },
            success(res){
              console.log("success",res)
              that.setData({
                  tea_name:res.result.data[0].user_name,
                  tea_id:res.result.data[0].teacher_id,
                  tea_xy:res.result.data[0].xy,
                  tea_level:res.result.data[0].level
              })
            },//成功的回调
            fail(res){
              console.log("fail",res)
            }//失败的回调
          })
      },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})